package org.ezplatform.office.meeting.info.dao;

import java.util.List;

import org.ezplatform.core.dao.jpa.JpaBaseDao;
import org.ezplatform.office.meeting.info.entity.ApplyRecord;
import org.ezplatform.office.meeting.setting.boardroom.entity.BoardroomSet;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface ApplyRecordDao extends JpaBaseDao<ApplyRecord, String> {
	
	@Query(value = "select id from ApplyRecord a where a.cycleId in (?1) and a.isCycle=1")
    List<String> getCycleMeetingids(List<String> ids);
	
	@Query(value = "from ApplyRecord a where (a.status=1 or a.status=4) and (a.meetingWay=1 or a.meetingWay=2) and startTime=?1 ")
    List<ApplyRecord> getWebApplyRecordList(String startTime);
	
    @Query(value = "from ApplyRecord a where a.status=?1")
    List<ApplyRecord> getApplyRecordList(int status);
    
    @Query(value = "from ApplyRecord a where a.cycleId=?1 and a.isSync=0 and a.corpId=?2")
    List<ApplyRecord> getApplyRecordListByCycleId(String cycleId, String corpId);
    
    @Query(value = "from ApplyRecord a where a.boardroomId=?1 and ((a.startTime between ?2 and ?3) or (a.endTime between ?2 and ?3) or (a.startTime <= ?2 and a.endTime >= ?3)) and a.status not in ('2','3','-1','6') order by a.startTime asc")
    List<ApplyRecord> getApplyRecordList(String boardroomSetId, String startDateStr, String endDateStr);
    
    @Query(value = "from ApplyRecord a where a.boardroomId in (?1) and ((a.startTime between ?2 and ?3) or (a.endTime between ?2 and ?3) or (a.startTime <= ?2 and a.endTime >= ?3)) and a.status not in ('0','2','3','-1','6') order by a.startTime asc")
    List<ApplyRecord> getApplyRecordList(List<String> boardroomSetId, String startDateStr, String endDateStr);

    @Query(value = "from ApplyRecord a where a.id not in(?1) and a.name=?2 and a.corpId=?3")
    List<ApplyRecord> getApplyRecordByName(String id, String checkdata, String corpId);

    @Query(value = "from ApplyRecord a where a.name=?1 and a.corpId=?2")
    List<ApplyRecord> getApplyRecordByName(String checkdata, String corpId);

    @Query(value = "from ApplyRecord a where a.id != ?1 and ((a.startTime >= ?2 and a.startTime < ?3) or (a.endTime > ?2 and a.endTime <= ?3) or (a.startTime <= ?2 and a.endTime >= ?3)) and a.corpId=?4 and a.boardroomId=?5 and a.status not in (-1,2,3,5,6)")
    List<ApplyRecord> getApplyRecordSeDate(String id, String startTime, String endTime, String corpId, String boardroomId);

    @Query(value = "from ApplyRecord a where ((a.startTime >= ?1 and a.startTime < ?2) or (a.endTime > ?1 and a.endTime <= ?2) or (a.startTime <= ?1 and a.endTime >= ?2)) and a.corpId=?3 and a.boardroomId=?4 and  a.status not in (-1,2,3,5,6)")
    List<ApplyRecord> getApplyRecordSeDate(String startTime, String endTime, String corpId,String boardroomId);
    
    @Query(value = "from ApplyRecord a where a.boardroomId in(?1)")
	List<ApplyRecord> getApplyRecordList(List<String> boardroomIdList);
    
    @Modifying
    @Query(value = "update ApplyRecord a set a.status = ?1 where a.id in (?2)")
    int updApplyRecord(int status, List<String> ids);
    
    @Modifying
    @Query(value = "update ApplyRecord set cycleId=?2 where id=?1")
    int updateCycleId(String applyRecordId, String cycleId);
    
    @Modifying
    @Query(value = "update ApplyRecord set isCycle=0,cycleId=null,cycleType=0,cycleValue=null,cycleEndTime=null where cycleId=?1")
    int updateIsCycle(String cycleId);
    
    @Modifying
    @Query(value = "delete from ApplyRecord a where a.id=?1")
    void deleteApplyRecord(String id);
    
    @Query(value = "from ApplyRecord b where id!=?4 and ((b.startTime<=?1 and b.endTime>= ?1) or (b.startTime <=?2 and b.endTime >=?2)) and b.boardroomId=?3 and  b.status not in (-1,2,3,5,6)")
    List<ApplyRecord> getApplyRecordByApp(String SDate,String EDate,String boardroomId,String applyRecordId);
    
}